import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import AddEditModal from '#cpt/temp/AddEditModal'
import TableControls from '#cpt/temp/TableControls'
import Controls from '#cpt/temp/Controls'
import Delete from '#cpt/temp/Delete'
import Enable from '#cpt/temp/Enable'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const Page = $Lazy.load(()=>import('#tp/page/page-container'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		// 搜索表单列表
		forms : [ 
			{ label: '办事处名称',	name: 'contact_name' },
			{ label: '联系电话',		name: 'contact_phone' },
			{ label: '创建时间',		names: ['start_date','end_date'], type:'date-range' },
		],
		// 提交表单列表
		formData : [
			{ label: '办事处名称',	name: 'contact_name', 			half:true,  required:true, },
			{ label: '联系电话',		name: 'contact_phone', 			half:true,  required:true },
			{ label: '位置选择',		name: 'areaData', 				half:true,  type: 'address', required:true, onRef: ref=> this.address = ref },
			{ label: '具体位置',		name: 'street', 				half:true,  required:true },
		],
		
	}
	
	model = {}
	
	componentDidMount(){
		if(window.isInit){
		  this.fetch()
		}
	}

	// paging
	fetch = () => $fn.fetch.call(this,'sp-box-address/index')
	// 表单
	cols = [
		{ title: '地址',			dataIndex: 'address', 			width:100 },
		{ title: '办事处名称', 		dataIndex: 'contact_name', 		width:150 },
		{ title: '联系电话', 		dataIndex: 'contact_phone',		...$fn.table.phone },
		{ title: '状态', 			dataIndex: 'enabled', 			...$fn.table.status,render: text => +text === 1 ? '启用' : '禁用' },
		{ title: '创建时间', 		dataIndex: 'created_at', 		...$fn.table.time },
		TableControls.controls.call(this,{ width: 200 })
	]
	// 表格操作按钮
	table_controls = (text,{ uuid, enabled }) => {
		return [
			TableControls.edit.call(this, 'sp-box-address/info', { uuid },{			// 编辑
				address: value => this.address.load(value)
			}),			
			this.onDel([ uuid ]),													// 删除
			+enabled === 0 && this.onEnabled(1, [ uuid ]),							// 启用
			+enabled === 1 && this.onEnabled(0, [ uuid ]),							// 禁用
		]
	}
	// 操作按钮
	controls = () => {
		return [
			Controls.add.call(this),			// 添加
			this.onDel(),						// 删除
			this.onEnabled(0),					// 禁用
			this.onEnabled(1),					// 启用
			Controls.imports.call(this,'sp-box-address/import', {op_key: 'SpAddressExcel', reconfirm: true}),		// 文件导入
			// 下载导入模板
			Controls.importTemplate.call(this),
		]
	}
	// 删除
	onDel = uuid => Delete.call(this, 'sp-box-address/delete', { uuid, isKeys: !uuid })
	// 禁用/启用
	onEnabled = (enabled, uuid) => Enable.call(this, enabled,  { uuid, on:'sp-box-address/on', off:'sp-box-address/off', isKeys: !uuid })
	
	render(){
		return (
			<>
				<Page title='物流办事处' controls={this.controls()}>
					{/* 搜索 */}
					{ SearchForm.call(this) }
					{/* 表格 */}
					{ Table.call(this, { isInit: window.isInit }) }
				</Page>
				{/* 添加、编辑 */}
				{ AddEditModal.call(this,{ labelWidth:'s7', width:800, api: 'sp-box-address/add', editApi: 'sp-box-address/edit'}) }
			</>
		)
	}
}

export default Index